package com.zl.smartmall.library.listener;

import java.util.List;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.po.MaintenancesProject;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 维修项目数据请求监听类
 * 
 * @author 陈希然
 * 
 */
public abstract class MaintenancesProjectListener extends BaseListener {
	private static final String TAG = MaintenancesProjectListener.class.getSimpleName();

	public MaintenancesProjectListener(RequestCode requestCode) {
		super(requestCode);
	}

	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

		try {
			int status = response.getInt(Constants.REQ_STATUS);
			String msg = response.getString(Constants.REQ_MSG);
			CrashHandler.d(TAG, "我的订单列表服务器status = " + status);
			CrashHandler.d(TAG, "我的订单列表服务器msg = " + msg);

			switch (status) {
			case Constants.REQ_OK:
				onResponseSuccess(MaintenancesProject.parse(response
						.getJSONArray("result")));
				break;

			case Constants.REQ_OFFLINE:
				onOffline();
				break;

			case Constants.REQ_NO_MORE_DATA:
				onNoMoreData();
				break;

			default:
				onResponseFailed();
			}

		} catch (JSONException e) {
			onFailure(statusCode, headers, e, new JSONObject());
		}
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
	}

	/**
	 * 我的订单列表数据请求成功
	 */
	public abstract void onResponseSuccess(
			List<MaintenancesProject> maintenancesProject);
}
